// ./mongodb/manager/townManager.js
const { TownModel: Town } = require("../../entity");

class TownManager {
  // 创建小镇
  static async createTown(data) {
    const town = new Town(data);
    return await town.save();
  }

  // 更新小镇
  static async updateTown(townId, updateData) {
    return await Town.findByIdAndUpdate(townId, updateData, {
      new: true,
    }).exec();
  }

  // 删除小镇（标记删除）
  static async softDeleteUser(townId) {
    return await Town.findByIdAndUpdate(
      townId,
      { deleted: true },
      { new: true },
    ).exec();
  }

  // 删除小镇（永久删除）
  static async deleteTown(userId) {
    return await Town.findByIdAndDelete(userId).exec();
  }

  // 查找小镇（根据小镇 ID）
  static async getTownById(townId) {
    return await Town.findById(townId).exec();
  }

  // 查找小镇（根据小镇名称）
  static async getTownByTownname(townname) {
    const town = await Town.findOne({ townname, deleted: false }).exec();
    if (!town) {
      throw new Error("Town not found");
    }
    return town;
  }

  // 查找小镇（获取所有未删除小镇）
  static async getAllActiveTowns() {
    return await Town.find({ deleted: false }).exec();
  }
}

module.exports = { TownManager };
